<template>
  <view class="router-view bcl-active">

<u-sticky>
    <u-tabs ref="tabs" :list="listtab" :current="current" active-color="red" @change="change"></u-tabs>
</u-sticky>    
    
      <view  class="container">
        <release-item v-for="(item, index) in list" :key="index" :item="item">
          <view slot="state">
            <text class="cl-main" v-if="item.status_name">{{ item.status_name}}</text>
          </view>
        </release-item>

        <!-- 数据状态 -->
        <uni-load-more
          :status="loadMoreState"
          @retry="getReleaseList(true)"
        ></uni-load-more>
      </view>

    <!-- <uni-tab
      :tab-title="tabTitle"
      :has-active-border="true"
      :currentIndex='1'
      @change="changeIndex"
    >
      <view slot="content" class="container">
        <release-item v-for="(item, index) in list" :key="index" :item="item">
          <view slot="state">
            <text class="cl-main" v-if="item.status_name">{{ item.status_name}}</text>
          </view>
        </release-item>

        <uni-load-more
          :status="loadMoreState"
          @retry="getReleaseList(true)"
        ></uni-load-more>
      </view>
    </uni-tab> -->
  </view>
</template>

<script>
import { myTask, acceptTask } from "@/apis/user";
import uniTab from "@/components/tab";
import releaseItem from "@/components/releaseItem";
import uniLoadMore from "@/components/loadMore";

export default {
  data() {
    return {
      pageStr: {
        page: 1,
        total: 0,
      },
      list: [],
      loadMoreState: 0,
      status: 7, // 全部
      isEngineer: false, // 是否工程师发布页面
      // list:["选标中", "进行中", "已完成", "退款", "投诉"],

				listtab: [{
					name: '选标中'
				}, {
					name: '进行中'
				}, {
					name: '已完成',
				}, {
					name: '修改中',
				}, {
					name: '退款',
				}, {
					name: '投诉',
				}],
				current: 0
    };
  },

  computed: {
    // tabTitle() {
    //   if (this.isEngineer) return ["选标中", "进行中", "已完成", "退款", "投诉"];
    //   else return ["选标中", "进行中", "已完成", "退款", "投诉"];
    // },
  },

  onLoad(options) {
    if (options.isEngineer == 1) {
      this.isEngineer = true;
      if(options.currentIndex == 2){
          this.status = 17
      this.current = 3
      }
      if(options.currentIndex == 1){
          this.status = 2
      this.current = 1
      }
      // this.$parent.$parent.tabBarIndex = 2
      // this.$parent.change()
      // this.status = 2
      console.log("this.$parent",this.$parent.$parent)
      uni.setNavigationBarTitle({
        title: "我是工程师",
      });
    }else{
      
      this.current = 0
      this.status = 2
    }

    this.getReleaseList();
  },

  methods: {
    /**
     * 获取发布列表
     */
    async getReleaseList(refresh = false) {
      if (refresh) {
        this.page = 1;
        this.list = [];
      }

      this.loadMoreState = 0;
      let params = {
        page: this.pageStr.page,
        status: this.status,
      };

      try {
        // myTask 我的发布接口， acceptTask 我是工程师接口
        const listFn = this.isEngineer ? acceptTask : myTask;

        let { code, data } = await listFn(params);
        if (code === 200) {
          if (refresh) {
            this.page = 1;
            this.list = [];
          }


          this.list = this.list.concat(data.data);
       
          this.pageStr.total = data.total;

          if (data.total == 0) this.loadMoreState = 5;
          else if (this.list.length < data.total) this.loadMoreState = 4;
          else if (this.list.length == data.total) this.loadMoreState = 3;
        } else this.loadMoreState = 2;
      } catch (_) {
        this.loadMoreState = 2;
      }
    },
    change(index){
      this.current = index;
      
      this.pageStr.page = 1;
      this.list = [];
      // 我是工程师/我的发布 状态
      let statusType = this.isEngineer
        ? {
            0: 15, // 选标中
            1: 2, // 进行中
            2: 12, // 已完成
            3: 17, // 修改中
            4: 18, // 退款
            5: 11, // 投诉
          }
        : {
            0: 2, // 选标中
            1: 1, // 进行中
            2: 12, // 已完成
            3: 17, // 修改中
			      4: 18, // 退款
            5: 11, // 投诉
          };
      this.status = statusType[index];
      this.getReleaseList();

    },

    changeIndex({ index, item }) {
      console.log(index)
      console.log(item)
      this.pageStr.page = 1;
      this.list = [];
      // 我是工程师/我的发布 状态
      let statusType = this.isEngineer
        ? {
            0: 7, // 选标中
            1: 1, // 进行中
            2: 12, // 已完成
            3: 18, // 退款
            4: 11, // 投诉
          }
        : {
            0: 7, // 选标中
            1: 15, // 进行中
            2: 4, // 已完成
			      3: 18, // 退款
            4: 11, // 投诉
          };
      this.status = statusType[index];
      this.getReleaseList();
    },
  },

  onReachBottom() {
    if (this.pageStr.total > this.list.length) {
      this.pageStr.page += 1;
      this.getReleaseList();
    }
  },

  components: {
    uniTab,
    releaseItem,
    uniLoadMore,
  },
};
</script>
<style lang="scss" scoped>
.router-view {
  &.bcl-active {
    background-color: $color-active !important;
  }
}
</style>
